﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;
using System.Data;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        Lorelist.aspx.cs           
/// Author :         Wangbc
/// CDT:             2013-02-01           
/// Version:         1.0.0.0                
/// Depiction:       知识库查询   
/// </summary>
public partial class main_admin_LoreList : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    private String EM;
    public string type_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.strHc = "0";
        //设置母版页控件显示
        ((Literal)Master.FindControl("lblpath")).Text = MenuPath;

        EM = Request.QueryString[WEBConstants.EM_QUERY_STRING];

        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        Master.sc = this.CreatSc(em);

        //只显示设定类型数据 type_id:1-产品部,2-市场部,3-培训部,19-网上申请     
        type_id = Util.objectToString(Request["type_id"]);
        if (type_id != "")
        {
            string type_code = string.Empty;
            if (type_id == "1") {
                type_code = "10";
            }
            else if (type_id == "2")
            {
                type_code = "11";
            }
            else if (type_id == "3")
            {
                type_code = "12";
            }
            else {
                type_code = "11020100";
            }

            //查询列表条件设置
            List<CDECriteria> list_Criteria = new List<CDECriteria>();
            Cit.CDE.Net.Module.CDECriteria dd = new CDECriteria("LORE_TYPE__TYPE_CODE", CDEConstCompare.LikeLeft, type_code);
            list_Criteria.Add(dd);
           

            //当前登陆人 数据权限处理
            LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
            //除产品部外，其它员工只能看自己所属区域数据
            if (op.POST == "0" && type_id != "1")
            {
                if (op.AREA_ID != "")
                {
                    Cit.CDE.Net.Module.CDECriteria areaCr = new CDECriteria("AREA_ID", CDEConstCompare.Equality, op.AREA_ID);
                    list_Criteria.Add(areaCr);
                } 
            }
            
            Context.Items["list_Criteria"] = list_Criteria;

            //下拉菜单EM条件设置
            Cit.CDE.Net.Module.CDECriteria username = new CDECriteria("TYPE_CODE", CDEConstCompare.LikeLeft, type_code);
            List<CDECriteria> list_Criteria_2 = new List<CDECriteria>();
            list_Criteria_2.Add(username);
            CDEThreadMap.setThreadMap("WEB.LORETYPE_DDL", list_Criteria_2);//传入下拉EM全名和条件LIST

        }


        //设置母版页控件事件
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ItemDataBound += new DataGridItemEventHandler(this.itemDataBound);


    }
    /// <summary>
    /// 操作事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void itemDataBound(object sender, DataGridItemEventArgs e)
    {
        //控制母版页控件显示内容
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns[((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns.Count - 1].Visible = true;
        if (e.Item.ItemIndex != -1)
        {
            DataTable table = ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataSource as DataTable;
            string ctcode = table.Rows[e.Item.ItemIndex]["LORE_TYPE__TYPE_CODE"].ToString();
            e.Item.Cells[2].Text = Util.getLoreTypeName(ctcode); 
        }
    }

    /// <summary>
    /// 删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdDel_Click(object sender, EventArgs e)
    {

        string strErr = string.Empty;
        string strMsg = string.Empty;
        
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];
        
        List<CDEEntity> listEntity = new List<CDEEntity>();
        foreach (int index in Master.listIndex)
        {
            S_listEntity[index].Operation = CDEDataOperation.UPDATE;
            S_listEntity[index].set("DEL_FLG", "1"); 
            listEntity.Add(S_listEntity[index]);
        }

        //更新
        CDEService.UpdateEntities(em, listEntity);
        //移除表格中的数据
        foreach (int index in Master.listIndex)
        {
            S_listEntity.Remove(S_listEntity[index]);
        }

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = S_listEntity;

        Session[WEBConstants.LIST_SESSION_ID + EM] = S_listEntity;

        Master.GetFirstNum();

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();

        for (int index = 0; index < listEntity.Count; index++)
        {
            if (listEntity[index].Result == CDEDataOperationResult.ERROR)
            {
                strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
            }
        }
 

        if (strErr != string.Empty)
        {
            strErr = strErr.Substring(0, strErr.Length - 1);
            strMsg = Resources.ResourceErrMsg.DELETE_ERR;
            strMsg = strMsg.Replace("{0}", strErr);
            showMessageErrAndOk(strMsg);
        }
        else
        {
            showMessageErrAndOk(Resources.ResourceAdmin.MSG_ROLE_DEL_RESULT_OK);
        }
        
        //重置页面条数
        Master.refreshPage();
        Master.refreshResult();
        //Master.dispSortImg();
        
    }
}
